import Vue from 'vue'
import VueRouter from 'vue-router'
//
import Login from "@/components/Login/Login";
import Home from "@/components/Home/Home";
import Page from "@/components/Page/Page";
import User from "@/components/User/User";
import Search from "@/components/Search/Search";
import Indent from "@/components/Indent/Indent";
import Edit from "@/components/User/Edit";
import Xiaoai from "@/components/User/Xiaoai";
import Xiaoinfo from "@/components/User/Xiaoinfo";




Vue.use(VueRouter)

const router = new VueRouter({
  routes:[
      // 重定向
    {path: '/', redirect: '/login'},
      // 路由规则
    {path:'/login',component:Login},
    {path:'/home',component:Home,
    children:[
      {path:'page',component:Page},
      {path:'search',component:Search},
      {path:'indent',component:Indent},
      {path:'user',component:User,},
    ]},
    {path:'/edit',component:Edit},
    {path:'/xiaoai',component:Xiaoai},
    {path:'/xiaoinfo',component:Xiaoinfo}

  ]
})

// 路由守卫
router.beforeEach(function (to,from,next) {
  const token = window.localStorage.getItem('token')
  if(token){
    next()
  }else {
    if(to.path==="/home"){
      alert("权限不够")
      next('/login');
    }else {
      next();
    }
  }
})

export default router
